﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace ShopB2C.DBUtilities.SqlDbHelper
{
    /// <summary>
    /// 静态 SqlTransaction 事务类
    /// </summary>
    public class SqlDbTransactions : DbBase
    {

        #region 事务

        // 静态变量
        private static DbTransactions<SqlConnection, SqlTransaction> _cacheSqlDbTransactions;
        // 静态构造函数
        static SqlDbTransactions()
        {
            SqlDbTransactions._cacheSqlDbTransactions =
                new DbTransactions<SqlConnection, SqlTransaction>();
        }
        /// <summary>
        /// 事务关联的 Conneciton 对象
        /// </summary>
        /// <returns>Conneciton</returns>
        public static SqlConnection ConnectionObject()
        {
            return SqlDbTransactions._cacheSqlDbTransactions.ConnectionObject();
        }
        /// <summary>
        /// 开始一个事务，并返回一个事务缓存字典的 key
        /// </summary>
        /// <returns>事务缓存字典的 key</returns>
        public static Guid BeginTransaction()
        {
            return SqlDbTransactions._cacheSqlDbTransactions.BeginTransaction(DbBase.ConnectionString);
        }
        /// <summary>
        /// 提交一个事务
        /// </summary>
        /// <param name="guid">事务缓存字典的 key</param>
        public static void CommitTransaction(Guid guid)
        {
            SqlDbTransactions._cacheSqlDbTransactions.CommitTransaction(guid);
        }
        /// <summary>
        /// 回滚一个事务
        /// </summary>
        /// <param name="guid">事务缓存字典的 key</param>
        public static void RollbackTransaction(Guid guid)
        {
            SqlDbTransactions._cacheSqlDbTransactions.RollbackTransaction(guid);
        }
        /// <summary>
        /// 返回一个事务缓存字典的 key的事务对象
        /// </summary>
        /// <param name="guid">事务缓存字典的 key</param>
        /// <returns>事务对象</returns>
        public static SqlTransaction Transaction(Guid guid)
        {
            return SqlDbTransactions._cacheSqlDbTransactions.Transaction(guid);
        }

        #endregion

        #region 执行 sql 语句

        /// <summary>
        /// 执行 sql 语句,返回指定的执行标志
        /// </summary>
        /// <param name="sqlString">sql 语句</param>
        /// <param name="guid">事务缓存字典的 key</param>
        /// <returns>执行标志</returns>
        public static int ExecuteNonQuery(string sqlString, Guid guid)
        {
            return SqlDbTransactions.ExecuteNonQueryText(sqlString, guid, null);
        }
        /// <summary>
        /// 执行一条计算查询结果语句,返回查询结果
        /// </summary>
        /// <param name="sqlString">sql 语句</param>
        /// <param name="guid">事务缓存字典的 key</param>
        /// <returns>查询结果</returns>
        public static object ExecuteScalar(string sqlString, Guid guid)
        {
            return SqlDbTransactions.ExecuteScalarText(sqlString, guid, null);
        }

        #endregion

        #region 执行带参数的 sql 语句

        /// <summary>
        /// 执行 sql 语句,返回指定的执行标志
        /// </summary>
        /// <param name="sqlString">sql 语句</param>
        /// <param name="guid">事务缓存字典的 key</param>
        /// <param name="sqlParams">sql参数组</param>
        /// <returns>执行标志</returns>
        public static int ExecuteNonQueryText(string sqlString, Guid guid, params SqlParameter[] sqlParams)
        {
            using (SqlCommand cmd = new SqlCommand(sqlString, SqlDbTransactions.ConnectionObject()))
            {
                cmd.Transaction = SqlDbTransactions.Transaction(guid);
                DbBase.PrepareCommand(cmd, sqlParams);
                return cmd.ExecuteNonQuery();
            }
        }
        /// <summary>
        /// 执行一条计算查询结果语句,返回查询结果
        /// </summary>
        /// <param name="sqlString">sql 语句</param>
        /// <param name="guid">事务缓存字典的 key</param>
        /// <param name="sqlParams">sql参数</param>
        /// <returns>查询结果</returns>
        public static object ExecuteScalarText(string sqlString, Guid guid, params SqlParameter[] sqlParams)
        {
            using (SqlCommand cmd = new SqlCommand(sqlString, SqlDbTransactions.ConnectionObject()))
            {
                cmd.Transaction = SqlDbTransactions.Transaction(guid);
                DbBase.PrepareCommand(cmd, sqlParams);
                object obj = cmd.ExecuteScalar();
                if (Object.Equals(obj, null) || Object.Equals(obj, DBNull.Value))
                {
                    return null;
                }
                else
                {
                    return obj;
                }
            }
        }

        #endregion

        #region 存储过程

        /// <summary>
        /// 执行存储过程,返回指定的执行标志
        /// </summary>
        /// <param name="procedure">存储过程名称</param>
        /// <param name="guid">事务缓存字典的 key</param>
        /// <param name="sqlParams">sql参数组</param>
        /// <returns>执行标志</returns>
        public static int ExecuteNonQueryProcedure(string procedure, Guid guid, params SqlParameter[] sqlParams)
        {
            using (SqlCommand cmd = new SqlCommand(procedure, SqlDbTransactions.ConnectionObject()))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Transaction = SqlDbTransactions.Transaction(guid);
                DbBase.PrepareCommand(cmd, sqlParams);
                return cmd.ExecuteNonQuery();
            }
        }
        /// <summary>
        /// 存储过程,返回查询结果
        /// </summary>
        /// <param name="procedure">存储过程名称</param>
        /// <param name="guid">事务缓存字典的 key</param>
        /// <param name="sqlParams">sql参数</param>
        /// <returns>查询结果</returns>
        public static object ExecuteScalarProcedure(string procedure, Guid guid, params SqlParameter[] sqlParams)
        {
            using (SqlCommand cmd = new SqlCommand(procedure, SqlDbTransactions.ConnectionObject()))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Transaction = SqlDbTransactions.Transaction(guid);
                DbBase.PrepareCommand(cmd, sqlParams);
                object obj = cmd.ExecuteScalar();
                if (Object.Equals(obj, null) || Object.Equals(obj, DBNull.Value))
                {
                    return null;
                }
                else
                {
                    return obj;
                }
            }
        }


        #endregion

    }
}
